package com.itheima.gongjulei;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class Demo01 {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1000);
        for (int i = 0; i < 500; i++) {
            Car car = new Car(semaphore);
            Thread t = new Thread(car, i + "号车");
            t.start();
        }

    }


}
class Car implements Runnable{
    private Semaphore semaphore;

    public Car(Semaphore semaphore) {
        this.semaphore= semaphore;
    }

    @Override
    public void run() {
        //等开门  获取信号量
        try {
            String name = Thread.currentThread().getName();
            System.out.println(name+"等待入场");
            semaphore.acquire();
            System.out.println(name+"开进来了");
            //停车
            Random r = new Random();
            int i = r.nextInt(24*3600*1000);
            System.out.println(name+"停留了"+i+"毫秒");
            Thread.sleep(i);
            //开出去 归还信号量
            System.out.println(name+"要开出去了");
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}